<?php
/**
 * 家庭成员
 *
 */
class FamilyMemberWidget extends Widget{

	public function render( $data ){
	    //获取家庭成员
        $intro = D('UserProfile', 'home')->getProfilesByModule($_SESSION['mid'], 'intro');
        $familyMembers = array();
        if (isset($intro['family'])) {
            $familyMembers = unserialize($intro['family']);
        }
		return $this->renderFile( $familyMembers );
	}
	/*
		Widget模版在第一次初始化的时候，写不进数据，故先改成如下形式。
	*/
	protected function renderFile( $familyMembers ){
	    $config = getFamilyConfig();
        $relation = $config['relation'];
        $duty = $config['duty'];
        $config = null;
        //var_dump($familyMembers, $duty);exit;
	    $out = '<div class="widget form_content families">
				<form action="'. U('home/account/update') . '" method="POST" id="family">
				<div class="body"><ul>
				<input type="hidden" name="dotype" value="upfamily" />';

	    $option = '';
	    foreach ($duty as $k => $v ) {
	        $option .= "<option value=\"{$k}\">{$v}</option>";
	    }

	    $out .= '<li class="main"><a href="#" class="avatar"><img src="'.__THEME__.'/image/a_son_128.png" title="宝宝" /></a>
			         	<span class="family_info">' .
			         	$_SESSION['uname'] . '<br/>
			         	职责:健康成长</span>
			         	</li>';
        foreach ($relation as $k => $v) {
            if (isset($familyMembers[$k])) {
                $out .= '<li class="">
				         	<a href="#" class="avatar"><img src="' . $familyMembers[$k]['avatar'] . '" title="' . $v[0] . '" /></a>
				         	<span class="family_info">
					         	<a href="#" class="icon yes">&nbsp;</a>' . $v[0] . '<br/>
					         	生日:<input class="date_selector" name="birthday[]" value="' . $familyMembers[$k]['birthday'] . '" size="" readonly/> <br/>
					         	职责:<select name="duty[]">
					         		' . $option . '
					         	</select>
					         	<input type="hidden" name="relation[]" value="'. $k . '" />
			         		</span>
			         	</li>';
            } else {
                $out .= '<li class="">
				         	<a href="#" class="avatar"><img src="'.__THEME__.'/image/' . $v[1] . '" title="' . $v[0] . '" /></a>
				         	<span class="family_info">
					         	<a href="#" class="icon yes">&nbsp;</a>' . $v[0] . '<br/>
					         	生日:<input class="date_selector" name="birthday[]" value="1950-01-01" size="" readonly/> <br/>
					         	职责:<select name="duty[]">
					         		' . $option . '
					         	</select>
					         	<input type="hidden" name="relation[]" value="'. $k . '" />
			         		</span>
			         	</li>';
            }

        }


        $out .=             '</ul>
			             <div class="form-actions">
				            <button type="submit" class="btn btn-primary">保存</button>
				          </div>
					</div>
					<!-- end form items-->
				</form>
			</div>';

		return	$out;
	}

}